package schoolsource.gui;

import java.util.Vector;

public class ListBox {

//    protected String boxTitle;
    protected Style boxStyle;
    protected Vector columns = new Vector();
    protected int columnLength = -1;
//    protected int boxTitleTop;
//    protected int boxTitleAlignment;

//    protected Dimension boxDimensions;
//    protected int boxTop;
//    protected int boxLeft;

//    protected Style boxHeadingStyle;
//    protected Style boxContentStyle;

//    protected List columns;

    public ListBox(Style style) {
        boxStyle = style;
    }

    public void addColumn(Column column) {
        if (columnLength == -1) {
            columnLength = column.getNumberOfRows();
            columns.add(column);
        }
        else if (columnLength == column.getNumberOfRows()) {
            columns.add(column);
        }
        else {
            // throw exception
        }
    }

    public String display() {
        String result = "";
        result += "              <div style=\"position:absolute;border-color: #000000; ";
        result += "border-width: 1px; border-style: solid; overflow:auto; ";
        result += boxStyle.getHTMLStyleString();
        result += "\">\n";
        // Print headers
        int offset = 0;
        for (int i = 0; i < columns.size(); ++i) {
            result += "<div STYLE=\"position:absolute;top:0;left:" ;
            result += offset + ";clip:rect(auto " + ((Column) columns.get(i)).getWidth();
            result += ((Column) columns.get(i)).getWidthType() + " auto auto);font-size:10;font-family:arial;\"><B>";
            result += ((Column) columns.get(i)).getHeader().getText() + "</B></div>\n";
            offset += ((Column) columns.get(i)).getWidth();
        }

        if (columns != null && columns.size() > 0) {
            String titleText;
            for (int i = 0; i < ((Column) columns.get(0)).getNumberOfRows(); ++i) {

                // Generate hover text
                titleText = "";
                for (int j = 0; j < columns.size(); ++j) {
                    titleText += ((Column) columns.get(j)).getText(i) + " ";
                }

                // Print row
                result += "<div TITLE=\"" + titleText + "\" STYLE=\"padding:1;position:absolute;top:"+((i+1)*1.5);
                // removed 'width:" + boxStyle.getWidth() + ";' from following line.
                result += "em;left:0;white-space:nowrap;\"  onClick=\"highlight(this,'Mail');";
                result += "switchGif(this.firstChild);setSelected(" + i + ");goToNextPage('" + FormFields.MAILLINK + "'";
                result += ", getSelected());\">"; // DO NOT PUT A LINEBREAK HERE -- IT COUNTS AS THE FIRST CHILD, SO THE IMAGE DOES NOT CHANGE
                offset = 0;
                for (int j = 0; j < columns.size(); ++j) {
                    if (((Column) columns.get(j)).useDiv()) {
                        result += "<div STYLE=\"padding:1;color:blue;position:absolute;top:0;" ;
                        result += "left:" + offset + ";clipWidth:" + (((Column) columns.get(j)).getWidth() - 5) + ";";
                        result += "width:" + ((Column) columns.get(j)).getWidth() + ";white-space:nowrap;";
                        result += "overflow:hidden;font-size:10;font-family:arial;\">";
                    }

                    result += ((Column) columns.get(j)).getRow(i, ((Column) columns.get(j)).getWidth());
                    if (((Column) columns.get(j)).useDiv()) {
                        result += "</div>";
                    }
                    result += "\n";

                    offset += ((Column) columns.get(j)).getWidth();
                }
                result += "</div>\n";
            }
        }

        result += "              </div>\n";
        return result;
    }

}